﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace RFPL
{
    public partial class PlayerInfo : Form
    {
        public int playerId = 0;
        private int fk_person = 0;
        private int fk_club = 0;
        private int fk_player_role = 0;

        // person data
        private int fk_country = 0;

        public PlayerInfo(int plId,int fk_cl)
        {
            InitializeComponent();

            playerId = plId;
            fk_club = fk_cl;
        }

        public void UpdateRoleList() {
            dgvRoles.Rows.Clear();

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[getAllRoles]";
            SqlDataReader dataReader = myCommand.ExecuteReader();
            while (dataReader.Read())
            {
                dgvRoles.Rows.Add(dataReader["id"],dataReader["name"]);
            }
            dataReader.Close();
            conn.Close();

            dgvRoles.ClearSelection();
        }

        private void PlayerInfo_Load(object sender, EventArgs e)
        {
            UpdateRoleList();

            if (playerId != 0) {

                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = Global.connectionString;
                conn.Open();
                SqlCommand myCommand = conn.CreateCommand();
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.CommandText = "[getPlayer]";
                myCommand.Parameters.Add("@id", SqlDbType.Int).Value = playerId;
                SqlDataReader dataReader = myCommand.ExecuteReader();
                if (dataReader.Read())
                {
                    if (!DBNull.Value.Equals(dataReader[0]))
                    {
                        playerPhoto.Image = ImageUtils.ByteArrayToImage((byte[])dataReader[0]);
                    }

                    playerName.Text = Convert.ToString(dataReader["name"]);

                    if (!DBNull.Value.Equals(dataReader["date"]))
                    {
                        dtmPlayer.Value = Convert.ToDateTime(dataReader["date"]);
                    }

                    if (!DBNull.Value.Equals(dataReader["fk_country"]))
                    {
                        fk_country = Convert.ToInt32(dataReader["fk_country"]);
                        UpdateCountry();
                    }

                    if (!DBNull.Value.Equals(dataReader["fk_player_role"]))
                    {
                        fk_player_role = Convert.ToInt32(dataReader["fk_player_role"]);
                        SetPlayerRole();
                    }
                }
                dataReader.Close();
                conn.Close();

            }

        }

        private void UpdateCountry()
        {

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[getCountry]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = fk_country;
            SqlDataReader dataReader = myCommand.ExecuteReader();
            if (dataReader.Read())
            {
                if (!DBNull.Value.Equals(dataReader["flag"]))
                {
                    flag.Image = ImageUtils.ByteArrayToImage((byte[])dataReader["flag"]);
                }
                country.Text = Convert.ToString(dataReader["name"]);
            }
            dataReader.Close();
            conn.Close();

        }

        private void SetPlayerRole()
        {

            foreach (DataGridViewRow row in dgvRoles.Rows) 
            {
                if (Convert.ToInt32(row.Cells["id"].Value) == fk_player_role)
                {
                    row.Selected = true;
                    break;
                }
            }

        }

        private void country_Click(object sender, EventArgs e)
        {
            CountrySelect cs = new CountrySelect();
            cs.ShowDialog();
            fk_country = cs.countryId;

            if (fk_country != 0)
            {
                UpdateCountry();
            }

            SetSaveState();
        }

        private void dgvRoles_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvRoles.SelectedRows.Count > 0)
            {
                edit.Enabled = true;
                del.Enabled = true;
            }
            else
            {
                edit.Enabled = false;
                del.Enabled = false;
            }

            SetSaveState();
        }

        private void add_Click(object sender, EventArgs e)
        {
            PlayerRoleInfo pi = new PlayerRoleInfo(0);
            pi.ShowDialog();

            UpdateRoleList();
        }

        private void edit_Click(object sender, EventArgs e)
        {
            int rlId = Convert.ToInt32(dgvRoles.SelectedRows[0].Cells["id"].Value);

            PlayerRoleInfo pi = new PlayerRoleInfo(rlId);
            pi.ShowDialog();

            UpdateRoleList();
        }

        private void del_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[delRole]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = fk_country;
            SqlDataReader dataReader = myCommand.ExecuteReader();
            if (dataReader.Read())
            {
                if (!DBNull.Value.Equals(dataReader["flag"]))
                {
                    flag.Image = ImageUtils.ByteArrayToImage((byte[])dataReader["flag"]);
                }
                country.Text = Convert.ToString(dataReader["name"]);
            }
            dataReader.Close();
            conn.Close();
        }

        private void save_Click(object sender, EventArgs e)
        {
            //editPlayer

            if (playerId == 0)
            {

                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = Global.connectionString;
                conn.Open();
                SqlCommand myCommand = conn.CreateCommand();
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.CommandText = "[addPerson]";

                if (playerPhoto.Image != null)
                {
                    byte[] fl = ImageUtils.ImageToByteArray(playerPhoto.Image);
                    myCommand.Parameters.Add("@photo", SqlDbType.VarBinary, fl.Length).Value = fl;
                }
                else
                {
                    myCommand.Parameters.Add("@photo", SqlDbType.VarBinary, 0).Value = DBNull.Value;
                }

                myCommand.Parameters.Add("@name", SqlDbType.NVarChar, playerName.Text.Length).Value = Convert.ToString(playerName.Text);

                myCommand.Parameters.Add("@date", SqlDbType.Date).Value = Convert.ToString(dtmPlayer.Value);

                if (fk_country != 0)
                {
                    myCommand.Parameters.Add("@fk_country", SqlDbType.Int).Value = fk_country;
                }
                else
                {
                    myCommand.Parameters.Add("@fk_country", SqlDbType.Int).Value = DBNull.Value;
                }
                                

                int persId = Convert.ToInt32( myCommand.ExecuteScalar());

                conn = new SqlConnection();
                conn.ConnectionString = Global.connectionString;
                conn.Open();
                myCommand = conn.CreateCommand();
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.CommandText = "[addPlayer]";

                myCommand.Parameters.Add("@fk_person", SqlDbType.Int).Value = persId;

                if (fk_club != 0)
                {
                    myCommand.Parameters.Add("@fk_club", SqlDbType.Int).Value = fk_club;
                }
                else
                {
                    myCommand.Parameters.Add("@fk_club", SqlDbType.Int).Value = DBNull.Value;
                }

                if (dgvRoles.SelectedRows.Count > 0)
                {
                    int fk_player_role = Convert.ToInt32(dgvRoles.SelectedRows[0].Cells["id"].Value);
                    myCommand.Parameters.Add("@fk_player_role", SqlDbType.Int).Value = fk_player_role;
                }

                myCommand.ExecuteScalar();

                conn.Close();

            }

            else {

                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = Global.connectionString;
                conn.Open();
                SqlCommand myCommand = conn.CreateCommand();
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.CommandText = "[editPlayer]";
                myCommand.Parameters.Add("@id", SqlDbType.Int).Value = playerId;

                if (playerPhoto.Image != null)
                {
                    byte[] fl = ImageUtils.ImageToByteArray(playerPhoto.Image);
                    myCommand.Parameters.Add("@photo", SqlDbType.VarBinary, fl.Length).Value = fl;
                }
                else
                {
                    myCommand.Parameters.Add("@photo", SqlDbType.VarBinary, 0).Value = DBNull.Value;
                }

                myCommand.Parameters.Add("@name", SqlDbType.NVarChar, playerName.Text.Length).Value = Convert.ToString(playerName.Text);

                myCommand.Parameters.Add("@date", SqlDbType.Date).Value = Convert.ToString(dtmPlayer.Value);

                if (fk_country != 0)
                {
                    myCommand.Parameters.Add("@fk_country", SqlDbType.Int).Value = fk_country;
                }
                else
                {
                    myCommand.Parameters.Add("@fk_country", SqlDbType.Int).Value = DBNull.Value;
                }

                if (fk_club != 0)
                {
                    myCommand.Parameters.Add("@fk_club", SqlDbType.Int).Value = fk_club;
                }
                else
                {
                    myCommand.Parameters.Add("@fk_club", SqlDbType.Int).Value = DBNull.Value;
                }
                if (dgvRoles.SelectedRows.Count > 0)
                {
                    int fk_player_role = Convert.ToInt32(dgvRoles.SelectedRows[0].Cells["id"].Value);
                    myCommand.Parameters.Add("@fk_player_role", SqlDbType.Int).Value = fk_player_role;
                }

                myCommand.ExecuteScalar();

                conn.Close();
            
            }

            this.Close();
        }

        private void SetSaveState() {

            if ((playerName.Text.Length > 0)&&(dgvRoles.SelectedRows.Count > 0)&&(fk_country!=0))
            {
                save.Enabled = true;
            }
            else {
                save.Enabled = false;
            }
        }

        private void playerName_TextChanged(object sender, EventArgs e)
        {
            SetSaveState();
        }

        private void playerPhoto_Click(object sender, EventArgs e)
        {
            ImageUtils.setImageFromStream(playerPhoto);
        }

    }
}
